package kr.co.handysoft.common.profiler;

import kr.co.handysoft.common.compress.Archiving;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.ProcState;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

public class CpuCommand extends AbstractCommand {

	@Override
	public String invoke() {
		
		Sigar sigar = new Sigar();
		this.command = "CPU";

		JSONObject cpuInfo = null;
		
		try {
			cpuInfo = new JSONObject();
			CpuInfo[] clist = sigar.getCpuInfoList();
	        ProcState procState = sigar.getProcState("$$");
	        cpuInfo.put("cpuInfo", JSONArray.fromObject(clist));
	        cpuInfo.put("procInfo", JSONArray.fromObject(procState));

		} catch (SigarException e) {
			e.printStackTrace();
        } finally {
        	sigar.close();
		}
		
		return Archiving.send(cpuInfo.toString());
	}

}
